<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--<script>-->
    <!--var mySignleton=function(){-->
        <!--/*这里声明私有变量和方法*/-->
        <!--var privateVariable='something private';-->
        <!--function showPrivate(){-->
            <!--console.log(privateVariable);-->
        <!--}-->
        <!--/*公有变量和方法(可以访问私有变量和方法)*/-->
        <!--return{-->
            <!--publicMethod:function(){-->
                <!--showPrivate();-->
            <!--},-->
            <!--publicVar:'the public can see this!'-->
        <!--}-->
    <!--}-->
    <!--var single=mySignleton();-->
    <!--single.publicMethod();-->
    <!--console.log(single.publicVar);-->
<!--</script>-->
<script>
    var Singleton = (function () {
        var instantiated;
        function init() {
            /*这里定义单例代码*/
            return {
                publicMethod: function () {
                    console.log('hello world');
                },
                publicProperty: 'test'
            };
        }

        return {
            getInstance: function () {
                if (!instantiated) {
                    instantiated = init();
                }
                return instantiated;
            }
        };
    })();

    /*调用公有的方法来获取实例:*/
    Singleton.getInstance().publicMethod();
</script>
</body>
</html>